package org.jeecg.modules.api.wlhy.customer;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerAccountBook;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerAccountBookService;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags="app货主多级账簿")
@RestController
@RequestMapping("/api/wlhy/customer/accountbook")
@Slf4j
public class ApiCustomerAccountBookController extends JeecgController<HyCustomerAccountBook, IHyCustomerAccountBookService> {
   @Autowired
   private IHyCustomerAccountBookService hyCustomerAccountBookService;

    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation(value="获取货主账户信息", notes="获取货主账户信息")
    @GetMapping(value = "/getAcc")
    public Result<?> getAcc() {
        Map data =  hyCustomerAccountBookService.getAcc();
        return Result.OK(data);
    }

    @ApiOperation(value="获取货主多级账簿信息", notes="获取货主多级账簿信息")
    @GetMapping(value = "/getAccountBookInfo")
    public Result<?> getAccountBookInfo() throws Exception {
        Map data =  hyCustomerAccountBookService.getAccountBookInfo();
        return Result.OK(data);
    }

    @AutoLog(value = "开通多级账簿")
    @ApiOperation(value="开通多级账簿", notes="开通多级账簿")
    @PostMapping(value = "/addAccountBook")
    public Result<?> addAccountBook(@RequestBody HyCustomerAccountBook hyCustomerAccountBook) {
//        if(StringUtils.isEmpty(hyCustomerAccountBook.getPayPwd())){
//            return Result.error("支付密码不能为空！");
//        }
        HyConfig config = HyConfigUtils.getConfig();
        if(!config.getNhbankIzUse()){
            return Result.error("未开通服务！");
        }
        hyCustomerAccountBookService.addAccountBook(hyCustomerAccountBook);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "设置支付密码")
    @ApiOperation(value="设置支付密码", notes="设置支付密码")
    @PostMapping(value = "/setpwd")
    public Result<?> setpwd(@RequestBody JSONObject jsonObject) {
        String oldPwd = jsonObject.getString("oldpassword");
        String password = jsonObject.getString("password");
        if(StringUtils.isEmpty(oldPwd)){
            return Result.error("原始支付密码不能为空！");
        }
        if(StringUtils.isEmpty(password)){
            return Result.error("新支付密码不能为空！");
        }
        //验证支付密码
        hyCustomerAccountBookService.editPayPwd(oldPwd,password);
        return Result.OK("修改成功！");
    }

    @AutoLog(value = "设置支付密码")
    @ApiOperation(value="设置支付密码", notes="设置支付密码")
    @PostMapping(value = "/resetpwd")
    public Result<?> resetpwd(@RequestBody JSONObject jsonObject) {
        String captcha = jsonObject.getString("captcha");
        String password = jsonObject.getString("password");
        if(StringUtils.isEmpty(captcha)){
            return Result.error("验证码！");
        }
        if(StringUtils.isEmpty(password)){
            return Result.error("新支付密码不能为空！");
        }
        LoginUser loginUser = UserUtil.getLoginUser();
        if(loginUser == null){
            return Result.error("未获取到登录信息");
        }
        String phone = loginUser.getPhone();
        Object code = redisUtil.get(phone);
        if (!captcha.equals(code)) {
            return Result.error("验证码错误");
        }
        //验证支付密码
        hyCustomerAccountBookService.ResetPayPwd(password);
        return Result.OK("修改成功！");
    }

}
